/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SuccessFrame.java
 *
 * Created on 2009-10-27, 20:48:26
 */
package View;

import Control.AirportServiceI;
import Control.FlightsServiceI;
import Model.AirportI;
import Model.FlightsI;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class SuccessFrame extends javax.swing.JFrame {

    private FlightsServiceI fsi;
    private ArrayList fligth_list = null;
    private AirportServiceI asi;
    private String user;
    /** Creates new form SuccessFrame */
    public SuccessFrame() {
        initComponents();
        initSearch();
    }

    public SuccessFrame(String user) {
        super("Success");
        initComponents();

        name_jlb.setText("Welcome,    " + user + "!"); //
        initList();
        initSearch();
        initAirportInformation();
        this.user=user;
    }

    public void initList() {
        fsi = new FlightsServiceI();
        fligth_list = (ArrayList) fsi.findAll();
        asi = new AirportServiceI();
    }

    public void initAirportInformation(){
        ArrayList airportList= (ArrayList)asi.findAll();
        for(int i=0;i<airportList.size();i++){
            if(SelectAirport_jcb.getSelectedItem().toString().equalsIgnoreCase(((AirportI)airportList.get(i)).getAirportName())){
                airport_name_jlb.setText("Airport Name: "+SelectAirport_jcb.getSelectedItem().toString()+" , ");
                airport_tax_jlb.setText("Airport Tax: "+((AirportI)airportList.get(i)).getAirportTax()+" , ");
                airport_gsd_jlb.setText("Airport GSD: "+((AirportI)airportList.get(i)).getAirportGSD()+" , ");
                airport_tel_jlb.setText("Airport Tel: "+((AirportI)airportList.get(i)).getAirportTel()+" . ");
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        logout_jbt = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        user_jlb = new javax.swing.JLabel();
        name_jlb = new javax.swing.JLabel();
        SelectFlights_jlb = new javax.swing.JLabel();
        destination_jlb = new javax.swing.JLabel();
        destination_jcb = new javax.swing.JComboBox();
        Ftime_jlb = new javax.swing.JLabel();
        Fdate_jlb = new javax.swing.JLabel();
        Ftime_jcb = new javax.swing.JComboBox();
        search_jbt = new javax.swing.JButton();
        admin_jbt = new javax.swing.JButton();
        manager_jbt = new javax.swing.JButton();
        AirportInformation_jlb = new javax.swing.JLabel();
        SelectAirport_jlb = new javax.swing.JLabel();
        SelectAirport_jcb = new javax.swing.JComboBox();
        select_jbt = new javax.swing.JButton();
        date_jtf = new javax.swing.JTextField();
        airport_name_jlb = new javax.swing.JLabel();
        airport_tax_jlb = new javax.swing.JLabel();
        airport_gsd_jlb = new javax.swing.JLabel();
        airport_tel_jlb = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        logout_jbt.setBackground(new java.awt.Color(51, 51, 51));
        logout_jbt.setForeground(new java.awt.Color(255, 255, 25));
        logout_jbt.setText("Logout");
        logout_jbt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logout_jbtActionPerformed(evt);
            }
        });

        SelectFlights_jlb.setText("3.Select your favorite flights in here:");

        destination_jlb.setText("Destinaton");

        destination_jcb.setBackground(new java.awt.Color(102, 102, 0));
        destination_jcb.setEditable(true);
        destination_jcb.setForeground(new java.awt.Color(153, 153, 153));
        destination_jcb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                destination_jcbActionPerformed(evt);
            }
        });

        Ftime_jlb.setText("Flight time");

        Fdate_jlb.setText("Flight date");

        Ftime_jcb.setBackground(new java.awt.Color(102, 102, 0));
        Ftime_jcb.setEditable(true);
        Ftime_jcb.setForeground(new java.awt.Color(153, 153, 153));
        Ftime_jcb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Ftime_jcbActionPerformed(evt);
            }
        });

        search_jbt.setBackground(new java.awt.Color(51, 51, 51));
        search_jbt.setForeground(new java.awt.Color(255, 255, 25));
        search_jbt.setText("Search");
        search_jbt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                search_jbtActionPerformed(evt);
            }
        });

        admin_jbt.setBackground(new java.awt.Color(51, 51, 51));
        admin_jbt.setForeground(new java.awt.Color(255, 255, 25));
        admin_jbt.setText("Admin");
        admin_jbt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                admin_jbtActionPerformed(evt);
            }
        });

        manager_jbt.setBackground(new java.awt.Color(51, 51, 51));
        manager_jbt.setForeground(new java.awt.Color(255, 255, 25));
        manager_jbt.setText("Manager");
        manager_jbt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                manager_jbtActionPerformed(evt);
            }
        });

        AirportInformation_jlb.setText("2.Airport infromation in here:");

        SelectAirport_jlb.setText("1.Select Airport:");

        SelectAirport_jcb.setBackground(new java.awt.Color(102, 102, 0));
        SelectAirport_jcb.setEditable(true);
        SelectAirport_jcb.setForeground(new java.awt.Color(153, 153, 153));
        SelectAirport_jcb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "BeiJing", "ShangHai", "GuangZhou", "Sydney" }));
        SelectAirport_jcb.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                SelectAirport_jcbMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                SelectAirport_jcbMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                SelectAirport_jcbMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                SelectAirport_jcbMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                SelectAirport_jcbMouseReleased(evt);
            }
        });
        SelectAirport_jcb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SelectAirport_jcbActionPerformed(evt);
            }
        });

        select_jbt.setBackground(new java.awt.Color(51, 51, 51));
        select_jbt.setForeground(new java.awt.Color(255, 255, 25));
        select_jbt.setText("Select");
        select_jbt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                select_jbtActionPerformed(evt);
            }
        });

        date_jtf.setText("YYYY-MM-DD");

        airport_name_jlb.setText("Airport Name:");

        airport_tax_jlb.setText("Airport Tax:");

        airport_gsd_jlb.setText("Airport GSD:");

        airport_tel_jlb.setText("Airport Tel:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(197, 197, 197)
                                .addComponent(user_jlb))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(admin_jbt, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(manager_jbt)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(name_jlb, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(299, 299, 299)
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(logout_jbt))
                            .addComponent(SelectFlights_jlb)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(destination_jlb)
                                .addGap(2, 2, 2)
                                .addComponent(destination_jcb, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Fdate_jlb)
                                .addGap(2, 2, 2)
                                .addComponent(date_jtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Ftime_jlb)
                                .addGap(6, 6, 6)
                                .addComponent(Ftime_jcb, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(search_jbt))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(SelectAirport_jlb)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(SelectAirport_jcb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(select_jbt)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(AirportInformation_jlb)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 175, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(airport_name_jlb)
                                .addGap(18, 18, 18)
                                .addComponent(airport_tax_jlb, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(airport_gsd_jlb, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(airport_tel_jlb)))
                        .addGap(300, 300, 300))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel1))
                        .addComponent(admin_jbt, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                        .addComponent(manager_jbt, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
                    .addComponent(name_jlb, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(user_jlb)
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SelectAirport_jlb)
                    .addComponent(SelectAirport_jcb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(select_jbt))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
                .addComponent(AirportInformation_jlb)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(airport_tax_jlb)
                    .addComponent(airport_name_jlb)
                    .addComponent(airport_gsd_jlb)
                    .addComponent(airport_tel_jlb))
                .addGap(36, 36, 36)
                .addComponent(SelectFlights_jlb)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(destination_jcb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(destination_jlb)
                    .addComponent(Fdate_jlb)
                    .addComponent(date_jtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Ftime_jlb)
                    .addComponent(Ftime_jcb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(search_jbt))
                .addGap(132, 132, 132))
            .addGroup(layout.createSequentialGroup()
                .addComponent(logout_jbt)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void initSearch() {
        destination_jcb.removeAllItems();
        Ftime_jcb.removeAllItems();
        String airport = SelectAirport_jcb.getSelectedItem().toString();
        FlightsI fi = null;
        if (fligth_list != null) {
            for (int i = 0; i < fligth_list.size(); i++) {
                fi = (FlightsI) fligth_list.get(i);

                if (fi != null && fi.getSairport().equals(airport)) {
                    destination_jcb.addItem(fi.getGairport());
                    Ftime_jcb.addItem(fi.getStime());
                }
            }
        }
    }

    public long computeDate(String after) {
        Date currentDate = new Date(System.currentTimeMillis());
        long day = -1;
        try {
            SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
            Date date = myFormatter.parse(after);
            day = (date.getTime() - currentDate.getTime()) / (24 * 60 * 60 * 1000);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            System.out.println("Methode SuccessFrame.computeDate() run failure");
            e.printStackTrace();
        }
        return day;
    }

    public FlightsI searchByUser(String planeid) {
        for (int i = 0; i < fligth_list.size(); i++) {
            if (((FlightsI) fligth_list.get(i)).getPlaneid().equals(planeid)) {
                return (FlightsI) fligth_list.get(i);
            }
        }
        return null;
    }
    private void logout_jbtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logout_jbtActionPerformed
        // TODO add your handling code here:
        this.dispose();
        LoginFrame login = new LoginFrame();
        login.setVisible(true);

    }//GEN-LAST:event_logout_jbtActionPerformed

    private void admin_jbtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_admin_jbtActionPerformed
        AdminFrame admin = new AdminFrame(user);
        admin.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_admin_jbtActionPerformed

    private void manager_jbtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_manager_jbtActionPerformed
        ManagerFrame manager = new ManagerFrame(user);
        manager.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_manager_jbtActionPerformed

    private void search_jbtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_search_jbtActionPerformed
        FlightsI fsti = new FlightsI();
        fsti.setSairport(SelectAirport_jcb.getSelectedItem().toString());
        fsti.setGairport(destination_jcb.getSelectedItem().toString());
        fsti.setStime(Ftime_jcb.getSelectedItem().toString());
        for(int i=0;i< fligth_list.size();i++){
            if( ((FlightsI)fligth_list.get(i)).getSairport().equalsIgnoreCase(fsti.getSairport())){
                if(((FlightsI)fligth_list.get(i)).getGairport().equalsIgnoreCase(fsti.getGairport())){
                    if(((FlightsI)fligth_list.get(i)).getStime().equalsIgnoreCase(fsti.getStime())){
                        fsti.setPlaneid(((FlightsI)fligth_list.get(i)).getPlaneid());
                        fsti.setGtime(((FlightsI)fligth_list.get(i)).getGtime());
                        fsti.setTicketprice(((FlightsI)fligth_list.get(i)).getTicketprice());
                      
                    }
                }
            }
        }
        if (computeDate(date_jtf.getText()) > 5 && computeDate(date_jtf.getText()) < 0) {
            String message = "You cann't buy" + computeDate(date_jtf.getText()) + " ticket,Please resetup time";
            JOptionPane.showMessageDialog(null, message);
            return;
        }
         
       
        //fsti.setGairport(destination_jcb)
        FlightFrame flight = new FlightFrame(fsti,user);
       
        flight.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_search_jbtActionPerformed

    private void destination_jcbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_destination_jcbActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_destination_jcbActionPerformed

    private void Ftime_jcbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Ftime_jcbActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_Ftime_jcbActionPerformed

    private void SelectAirport_jcbMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SelectAirport_jcbMouseExited

        // TODO add your handling code here:
        initSearch();
    }//GEN-LAST:event_SelectAirport_jcbMouseExited

    private void SelectAirport_jcbMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SelectAirport_jcbMouseClicked
        // TODO add your handling code here:
        JOptionPane.showMessageDialog(null, "Running");
    }//GEN-LAST:event_SelectAirport_jcbMouseClicked

    private void SelectAirport_jcbMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SelectAirport_jcbMouseEntered
        // TODO add your handling code here:
        JOptionPane.showMessageDialog(null, "Running");
    }//GEN-LAST:event_SelectAirport_jcbMouseEntered

    private void SelectAirport_jcbMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SelectAirport_jcbMousePressed
        // TODO add your handling code here:
        JOptionPane.showMessageDialog(null, "Running");
    }//GEN-LAST:event_SelectAirport_jcbMousePressed

    private void SelectAirport_jcbMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SelectAirport_jcbMouseReleased
        // TODO add your handling code here:
        JOptionPane.showMessageDialog(null, "Running");
    }//GEN-LAST:event_SelectAirport_jcbMouseReleased

    private void SelectAirport_jcbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SelectAirport_jcbActionPerformed
        // TODO add your handling code here:
        initSearch();
        initAirportInformation();
    }//GEN-LAST:event_SelectAirport_jcbActionPerformed

    private void select_jbtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_select_jbtActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_select_jbtActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel AirportInformation_jlb;
    private javax.swing.JLabel Fdate_jlb;
    private javax.swing.JComboBox Ftime_jcb;
    private javax.swing.JLabel Ftime_jlb;
    private javax.swing.JComboBox SelectAirport_jcb;
    private javax.swing.JLabel SelectAirport_jlb;
    private javax.swing.JLabel SelectFlights_jlb;
    private javax.swing.JButton admin_jbt;
    private javax.swing.JLabel airport_gsd_jlb;
    private javax.swing.JLabel airport_name_jlb;
    private javax.swing.JLabel airport_tax_jlb;
    private javax.swing.JLabel airport_tel_jlb;
    private javax.swing.JTextField date_jtf;
    private javax.swing.JComboBox destination_jcb;
    private javax.swing.JLabel destination_jlb;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton logout_jbt;
    private javax.swing.JButton manager_jbt;
    private javax.swing.JLabel name_jlb;
    private javax.swing.JButton search_jbt;
    private javax.swing.JButton select_jbt;
    private javax.swing.JLabel user_jlb;
    // End of variables declaration//GEN-END:variables
}
